package org.ysu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ysu.pojo.AddressList;
import org.ysu.pojo.Region;
import org.ysu.service.RegionService;
import org.ysu.mapper.RegionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author libaoyu
* @description 针对表【region】的数据库操作Service实现
* @createDate 2024-06-17 14:59:36
*/
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region>
    implements RegionService{
    @Resource
    RegionMapper regionMapper;
    @Override
    public String getByRid(Integer rid) {
        String address="";
        while(rid!=0) {
            Region region = regionMapper.selectById(rid);
            address=region.getName()+address;
            rid=region.getUpper();
        }
        return address;
    }

    @Override
    public AddressList getAddress(Integer level, String place) {
        AddressList addressList=new AddressList();
        //如果level为1，则返回省份
        if(level==1) {
            //找到所有upper为0的rid和name
            List<Integer> ids = regionMapper.getAddress(0);
            addressList.setIds(ids);
            //获取所有id
            List<String> names = regionMapper.getAddressesById(ids);
            addressList.setNames(names);
            return addressList;
        }
        if(level==2 || level==3) {
            //找到place对应的rid
            Integer rid = regionMapper.getRid(place);
            //找到所有upper为rid的id和name
            List<Integer> ids = regionMapper.getAddress(rid);
            addressList.setIds(ids);
            //获取所有id
            List<String> names = regionMapper.getAddressesById(ids);
            addressList.setNames(names);
            return addressList;
        }
        return null;
    }
}




